package com.danton.user.service.impl;

import java.util.List;

import com.danton.common.utils.StringUtils;
import com.danton.model.BsAdminRoleModel;
import com.danton.model.BsRoleModel;
import com.danton.model.BsRolePermissionModel;
import com.danton.user.service.RoleService;
import com.jfinal.plugin.activerecord.Page;

public class RoleServiceImpl implements RoleService {

	@Override
	public Page<BsRoleModel> list(int pageNumber, int pageSize) {
		return BsRoleModel.dao.paginate(pageNumber, pageSize);
	}

	@Override
	public boolean add(BsRoleModel roleModel) {
		String name = String.valueOf(roleModel.get("name"));
		String id = findId(name);
		if (id == null || ("".equals(id))) {
			return roleModel.save();
		}
		return false;
	}

	@Override
	public boolean edit(BsRoleModel roleModel) {
		String role_id = String.valueOf(roleModel.get("role_id"));
		String name = String.valueOf(roleModel.get("name"));
		boolean flag = findByIdAndName(name, role_id);
		if (flag) {
			return roleModel.update();
		}
		return false;
	}

	@Override
	public Page<BsRoleModel> searchRole(int pageNumber, int pageSize, String kw) {
		return BsRoleModel.dao.paginateByKw(pageNumber, pageSize, kw);
	}

	@Override
	public String findId(String role_name) {
		String sql = "select id from bs_role where name=" + "'" + role_name + "'";
		List<BsRoleModel> rolelist = BsRoleModel.dao.find(sql);
		String id = null;
		for (BsRoleModel role : rolelist) {
			id = String.valueOf(role.get("id"));
		}
		return id;
	}

	@Override
	public boolean relatePermissions(List<BsRolePermissionModel> rplist) {

		for (BsRolePermissionModel rp : rplist) {
			boolean flag = rp.save();
			if (!flag) {
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean relateUsers(List<BsAdminRoleModel> rulist) {
		for (BsAdminRoleModel rp : rulist) {
			boolean flag = rp.save();
			if (!flag) {
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean deletePermissions(int role_id) {
		String sql = "select * from bs_role_permission where role_id=" + "'" + role_id + "'";
		List<BsRolePermissionModel> list = BsRolePermissionModel.dao.find(sql);
		if (list != null) {
			for (BsRolePermissionModel rpm : list) {
				boolean flag = rpm.delete();
				if (!flag)
					return false;
			}
		}
		return true;
	}

	@Override
	public boolean findByIdAndName(String role_name, String role_id) {
		String sql = "select * from bs_role where id=" + "'" + role_id + "'" + " and name=" + "'" + role_name + "'";
		List<BsRoleModel> list = BsRoleModel.dao.find(sql);
		if (list.size() == 0) {
			return true;
		}
		return false;
	}

	@Override
	public void deleteRoleAdminByUserId(String user_ids) {
		String[] userid_arr = user_ids.split(",");// 过滤掉空格
		String ids = "";
		for (String id : userid_arr) {
			if (StringUtils.isNotEmpty(id)) {
				ids += id + ",";
			}
		}
		if (StringUtils.isNotEmpty(ids)) {
			ids = ids.substring(0, ids.length() - 1);
		}
		String sql = "select * from bs_admin_role where admin_id in(" + ids + ")";
		List<BsAdminRoleModel> arlist = BsAdminRoleModel.dao.find(sql);
		if (arlist.size() > 0) {
			for (BsAdminRoleModel arm : arlist) {
				if (arm != null) {
					arm.delete();
				}
			}
		}
	}

	@Override
	public List<BsRoleModel> listAllRoles() {
		return BsRoleModel.dao.listAllRoles();
	}

	@Override
	public List<BsRoleModel> listUserRoles(int user_id) {
		return BsRoleModel.dao.listUserRoles(user_id);
	}

	@Override
	public boolean roleStatusCheck(int role_id) {
		return BsRoleModel.dao.roleStatusCheck(role_id);
	}

	@Override
	public BsRoleModel getRoleById(int role_id) {
		return BsRoleModel.dao.getRoleById(role_id);
	}

	@Override
	public boolean editRoleStatus(BsRoleModel rm) {
		if(rm!=null){
			return rm.update();
		}
		return false;
	}

}
